package com.cxw.healthserver.service

import com.cxw.healthserver.entity.Exam
import com.cxw.healthserver.entity.Follow
import com.cxw.healthserver.mapper.FollowMapper
import com.cxw.healthserver.response.BaseResponse
import org.json.JSONObject
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
 * @createTime 2024/6/28 23:38
 * @author 崔兴旺
 * @description
 */
@Service
class FollowService : BaseService<Follow>() {
    @Autowired
    lateinit var followMapper: FollowMapper


    fun saveFollow(username: String, jsonStr: String): BaseResponse<Any> {
        val jsonObject = JSONObject(jsonStr)
        val sourceTableId: String = jsonObject.getString("sourceTableId")
        val followCode: Int = jsonObject.getInt("followCode")
        val idNumber = jsonObject.optString("idNumber", "")
        val queryWrapper = createQueryWrapper(username, idNumber)
        var follow = followMapper.selectOne(
            queryWrapper.eq("sourceTableId", sourceTableId)
        )
        var isAdd = false
        if (follow == null) {
            follow = Follow()
            follow.username = username
            follow.idNumber = idNumber
            follow.sourceTableId = sourceTableId
            follow.followCode= followCode
            isAdd = true
        }
        follow.json = jsonStr
        if (isAdd) {
            followMapper.insert(follow)
        } else {
            followMapper.updateById(follow)
        }
        return BaseResponse.success(null)
    }
}